package com.dlc.felear.xiaoensale.helper;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.dlc.felear.xiaoensale.utils.ConvertUtils;

/**
 * Session存储
 */
public class Session {
    private static final String TAG="Session";
    private SharedPreferences sp;
    private static Session session;
    public static void init(Context context, String name){
        if(session==null){
            synchronized (Session.class){
                if(session==null){
                    session=new Session(context,name);
                }
            }
        }
    }

    public static Session getSession() {
        Log.e(TAG,"session="+session);
        return session;
    }

    private Session(Context context, String name) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        Log.e(TAG,"初始化成功");
    }

    /**
     * 移除
     */
    public void remove(String key) {
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    /**
     * 设置int值
     */
    public void setInt(String key, int value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    /**
     * 获取int值
     *
     * @param key
     */
    public int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * 获取int值
     *
     * @param key
     * @param def
     */
    public int getInt(String key, int def) {
        return sp.getInt(key, def);
    }

    /**
     * 设置boolean值
     *
     * @param key
     * @param value
     */
    public void setBoolean(String key, boolean value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 获取boolean值
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 获取boolean值
     *
     * @param key
     * @param def
     */
    public boolean getBoolean(String key, boolean def) {
        return sp.getBoolean(key, def);
    }

    /**
     * 设置字符串
     *
     * @param key
     * @return
     */
    public void setString(String key, String value) {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 获取字符串
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return getString(key, null);
    }

    /**
     * 获取字符串
     *
     * @param key
     * @param def
     * @return
     */
    public String getString(String key, String def) {
        return sp.getString(key, def);
    }

    /**
     * 存储对象
     *
     * @param key
     * @param o
     */
    public void setEntity(String key, Object o) {
        setString(key, ConvertUtils.object2Base64(o));
    }

    /**
     * 获取对象
     *
     * @param key
     * @return
     */
    public Object getEntity(String key) {
        return ConvertUtils.base642Object(getString(key));
    }

}
